package com.minisoft.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.apache.rocketmq.acl.common.AclClientRPCHook;
import org.apache.rocketmq.acl.common.SessionCredentials;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author JueSu
 * @description: TODO
 * @date 2021/9/16 19:06
 */

public class RabbitMQTest {

    @Test
    public void testSend() {
        String body = "{\"/dhyx/DhProjectSalespersonAction/saveSalerUser\":\"{\\\"passport\\\":\\\"506d69f88b8a85ea554f22bfdf4fdb68\\\",\\\"openid\\\":\\\"oL73aw9Pq2nUh4iA1a0dVn8rCULc\\\",\\\"action\\\":\\\"/dhyx/DhProjectSalespersonAction/saveSalerUser\\\",\\\"requestParam\\\":{\\\"obj\\\":{\\\"awardtype\\\":2,\\\"usersrc\\\":2,\\\"channel\\\":\\\"来访\\\",\\\"mark\\\":0,\\\"saleropenid\\\":\\\"omkQy0kEfqK7rNmGgd7kGWtOMQ2A\\\"},\\\"sex\\\":\\\"男\\\",\\\"name\\\":\\\"连先生\\\",\\\"userKey\\\":\\\"HFGC\\\",\\\"mobile\\\":\\\"18862383330\\\",\\\"projectId\\\":3494,\\\"visitorId\\\":555658},\\\"vericode\\\":\\\"\\\",\\\"url\\\":\\\"http://zyhfsz.rushiai.com/ipad/checkin2/index.html#/layout/xsfp?isNewBind=2&vid=555658\\\"}\"}";
        body = "{\"/dhyx/DhProjectSalespersonAction/sendWxMsg\":\"{\\\"passport\\\":\\\"093b0c5e0a3f6359afbabc9d19cdddac\\\",\\\"openid\\\":\\\"omkQy0mrzAfX6ZliXsVEiI02QSGU\\\",\\\"action\\\":\\\"/dhyx/DhProjectSalespersonAction/sendWxMsg\\\",\\\"requestParam\\\":{\\\"mobile\\\":\\\"13702771007\\\",\\\"salerUserId\\\":417585,\\\"projectId\\\":3356,\\\"userKey\\\":\\\"HFGC\\\"},\\\"vericode\\\":\\\"\\\",\\\"url\\\":\\\"http://zhhfzygj.rushiai.com/ipad/checkin2/index.html#/layout/qdqr\\\"}\"}";
        body = "{\"/dhyx/DhProjectSalespersonAction/saveSalerUser\":\"{\\\"passport\\\":\\\"0784586fa256ce4447e75244a08e1905\\\",\\\"openid\\\":\\\"oha-KxHBlka26N3onxJP4w6mOrm4\\\",\\\"action\\\":\\\"/dhyx/DhProjectSalespersonAction/saveSalerUser\\\",\\\"requestParam\\\":{\\\"obj\\\":{\\\"awardtype\\\":2,\\\"usersrc\\\":2,\\\"channel\\\":\\\"来访\\\",\\\"mark\\\":0,\\\"saleropenid\\\":\\\"oha-KxHV7HURSkVSGFki9a0Ug8gU\\\"},\\\"sex\\\":\\\"男\\\",\\\"name\\\":\\\"王\\\",\\\"mobile\\\":\\\"15726668870\\\",\\\"projectId\\\":null,\\\"userKey\\\":\\\"chenling1\\\",\\\"visitorId\\\":556261},\\\"vericode\\\":\\\"\\\",\\\"url\\\":\\\"http://zysyfwh.rushiai.com/ipad/checkin2/index.html#/layout/xsfp?isNewBind=2&vid=556261\\\"}\"}";
        sendMqMessage(body,"gf_customer_center_validation");
    }

    private static final String EXCHANGE_NAME = "rushi_exchange_direct";
    public static final String ROUTING_KEY = ".test";

    private void sendMqMessage(String message, String queueName) {
        try {

            Connection connection = getConnection();
            Channel channel = connection.createChannel();

            channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);

            channel.basicPublish(EXCHANGE_NAME, queueName + ROUTING_KEY, null, message.getBytes());

            channel.close();
            connection.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        //原圈测试环境
        factory.setHost("121.37.29.92");
        factory.setPort(5672);
        factory.setVirtualHost("/test");
        factory.setUsername("rabbitmq");
        factory.setPassword("rabbitmq@123");

        return factory.newConnection();
    }

    @Test
    public void testRockSend() throws Exception {
        AclClientRPCHook aclClientRPCHook = new AclClientRPCHook(new SessionCredentials("gf-customer-center", "gf-customer-center"));

        DefaultMQProducer producer = new DefaultMQProducer("gfCustomerCenterGroup", aclClientRPCHook);
        producer.setNamesrvAddr("172.16.57.154:9876;172.16.57.153:9876");
        producer.start();
        Message message = new Message();
        message.setTopic("syncCustomerCenterDataTopicDev");
        message.setTags("tags");
        String bodyStr = "{\"action\":\"updateCustomer\",\"params\":{\"address\":\"\",\"ageStageName\":\"41-45\",\"buyfactor\":\"户型,品牌,地段,周边环境\",\"carBrandName\":\"\",\"carParkName\":\"\",\"channelWayName\":\"电销邀约\",\"contactTypeName\":\"来访\",\"customerFollow\":[],\"customerName\":\"王先生\",\"customerOpportunityId\":\"1363137\",\"description\":\"客户目前住在攀成钢，锦江区工作，认可产品，认为我们地段有点偏\",\"educationName\":\"\",\"familyIncomeName\":\"\",\"familyMemberName\":\"1小孩\",\"gender\":0,\"idCardNo\":\"\",\"idCardType\":\"\",\"isLimitedBuy\":\"\",\"isLimitedLoan\":\"\",\"liveplate\":\"\",\"mobile\":\"13881800566\",\"mobile01\":\"\",\"mobile02\":\"\",\"notbuyreason\":\"价格太高,路段太偏\",\"opportunityState\":2,\"professionName\":\"\",\"propcondition\":\"\",\"saleMainId\":\"lijia\",\"saleOrgId\":\"1210\",\"targetAcreageName\":\"200-250㎡\",\"targetHouseTypeName\":\"\",\"targetObjectiveName\":\"自住\",\"targetTotalPriceName\":\"700-800万\",\"targetUnitPriceName\":\"\",\"tradeName\":\"建筑\",\"userrank\":\"C类\",\"vehicleName\":\"\",\"visitChannelName\":\"自然到访\",\"visitFirstDate\":1632035911000,\"workAreaName\":\"锦江区\"},\"saleTeamSaleOrgRel\":[\"1012\",\"1211\"]}";
        bodyStr = "{\"action\":\"inform\",\"params\":{\"customerAllocationDto\":{},\"customerMobile\":\"13625251144\",\"customerOpportunityId\":\"6535cabe8be54c898f7e60478da954a8\",\"reportId\":\"6527817393cb4f9faf53c1e2f0e0a40c\",\"saleMainId\":\"LIJIA\",\"saleMainMobile\":\"13727095795\",\"saleTeamId\":\"1000118\",\"updateId\":\"zhysj\",\"visitLastDate\":1632823402000},\"saleTeamSaleOrgRel\":[\"1012\"]}";
        message.setBody(bodyStr.getBytes(StandardCharsets.UTF_8));
        producer.send(message);
        System.out.println("send ok");
    }
}
